﻿using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using OWASP.CodeReview.CodeCrawler.Functionalities.Reports.Templates;

namespace CodeCrawler.UI.Functionalities.Reporting {
    /// <summary>
    /// OWASP Code Crawler Reporting Functionality
    ///     
    /// This class is responsible for the reporting area within Code Crawler.
    /// It can generate RTF and HTML reports using Net.Sgoliver.NRtfTree.Util and XML/XSL.
    ///
    /// </summary>
    public static class Reporting {
        /// <summary>
        /// Create RTF Report, specifies the location of the related dump file.
        ///                    also specifies the output path
        /// </summary>
        public static string GenerateRTF(string selectedDumpFile, string reportName) {

            string rtfReportPath;

            StringBuilder dumpFolderFileLocation;
            CreateDumpAndReportFolderPath(out rtfReportPath);

            // RTF Report Generator
            var rtfGen = new rtfTemplate();

            string reportPath   = string.Concat(Application.StartupPath, "/Results/Generated/");
             
            string result = rtfGen.Do(selectedDumpFile, reportPath, string.Concat(reportName, ".rtf"));

            if (!string.IsNullOrEmpty(result)) return result;
            MessageBox.Show("Error occurred while generating a report...");
            return result;
        }

        /// <summary>
        /// This code has to be improved for 100% release
        /// </summary>
        public static string GenerateHTML(string dumpFilePath,string TemplatePath,string reportName) {
            string reportsDirectory = string.Concat(Application.StartupPath, "/Results/Generated/");
            DirectoryInfo outputDirectoryName = new DirectoryInfo(reportsDirectory);

            if (!outputDirectoryName.Exists) { outputDirectoryName.Create(); } 
            
            StringBuilder xmlOutputThreatList = new StringBuilder();
            xmlOutputThreatList.Append(reportsDirectory);
            xmlOutputThreatList.Append("/");
            xmlOutputThreatList.Append(reportName.Trim().ToLower());
            xmlOutputThreatList.Append(".xml");

            xmlTemplate Generator    = new xmlTemplate();
            Generator.DoXSLCompiledTransformation(dumpFilePath, TemplatePath, reportsDirectory + reportName.Trim() + ".html");

            return reportsDirectory + reportName.Trim() + ".html";
        }

        /// <summary>
        /// Set rtfReportPath and dumpFolderFileLocation with their relative
        /// path locations
        /// </summary>
        /// <param name="rtfReportPath"></param>
        /// <param name="dumpFolderFileLocation"></param>
        private static void CreateDumpAndReportFolderPath(out string rtfReportPath) {
            rtfReportPath = string.Concat(Application.StartupPath.ToString(), "/Results/RTF/");
        }

        /// <summary>
        /// Read one single scan generated in the past
        /// </summary>
        /// <param name="singleScanFileDump">FileInfo</param>
        /// <returns>Array</returns>
        public static string[] ReadSingleFileDump(FileInfo singleScanFileDump) {

            int DumpVulnerabilities         = 0;
            string CreatedDateTime          = string.Empty;

            string DateTime = string.Empty;

            // Get Vulnerabilities count by reading file content using System.XML;
            XmlTextReader settingsReader = new XmlTextReader(singleScanFileDump.FullName);
            while (settingsReader.Read()) {

                XmlNodeType nType = settingsReader.NodeType;
                if (nType == XmlNodeType.Element) { DumpVulnerabilities++; }
            }

            CreatedDateTime                 = singleScanFileDump.CreationTime.ToString();
            string singleScanFullPath       = singleScanFileDump.FullName;
            
            if ((!string.IsNullOrEmpty(CreatedDateTime)) && (DumpVulnerabilities > 0)) {

                string[] DumpsListViewItems = new string[] { singleScanFileDump.Name, DumpVulnerabilities.ToString(), CreatedDateTime, singleScanFullPath };
                return DumpsListViewItems;
            }
            else { return null; }
        }
    }
}